<?php
namespace app\system\controller;

use app\system\model\ContentclassModel;
use app\system\model\ContentModel;

/**
 * 广告内容
 *
 * @ author zy <741599086@qq.com>
 * */
 
class Content extends Common
{
    /**
     * 列表
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
	public function index() 
	{
		$equalField = [ 'contentclass_id', 'type' , 'state' ] ;  //恒等于搜索的字段
		$likeField = 'title'; //模糊搜索的字段,用|分割
		$keywords = input('get.keywords', '', 'trim') ;
		
		$where = [] ;
		foreach ($equalField as $v) {
			$tmp = input('get.' . $v, 0, 'intval');
			$tmp != '' && $where[$v] = $tmp ;
		}
		$keywords && $where['keywords'] = ['like', "%$keywords%"];

		$model = new ContentModel();
		$list = $model->selectContent($where, 20, 'sort desc,id desc');

        $classModel = new ContentclassModel();
		$contentClassList = $classModel->order ('sort desc,id desc')->column('id,title');

        $assign = [
            'contentClassList' => $contentClassList,
            'list' => $list
        ];
		return $this->fetch ('', $assign);
	}


    /**
     * 添加
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
	public function add()
	{

		if ( isAjaxPost() ) {
		
			$data = input('post.') ;
			 
			if(in_array($data['type'] , [1,2,3]) && empty($data['content'])) {
				return $this->jsonError('请输入主要内容') ;
			}
			 
			if($data['type'] == 4){ //单图

				$data['content']['img'] = $data['img'];
				if (empty($data['content']['img'])) {
                    return $this->jsonError('请上传图片');
                }
				$data['content']['url'] = $data['url'];
                unset($data['url'], $data['img']);
				$data['content'] = serialize($data['content']);
			}
			 
			if($data['type'] == 5){ //多图
                $data['content'] = [];
                $sort = [];
                if (empty($data['imgs'][0])) {
                    return $this->jsonError('请上传图片');
                }
                foreach ($data['imgs'] as $k=>$v) {
                    if (!empty($v)) {
                        $data['content'][$k] = [
                            'img' => $v,
                            'url' => trim($data['urls'][$k]),
                            'sort' => intval($data['sorts'][$k])
                        ];
                        $sort[$k] = $data['sorts'][$k];
                    }
                }
				array_multisort($sort, SORT_ASC, $data['content']);
                unset($data['imgs'], $data['urls'], $data['sorts']);
				 
				$data['content'] = serialize($data['content']);
				 
			}

            $validate = new \app\system\validate\Content();
            if (! $validate->check($data)) {
                return $this->jsonError($validate->getError());
            }

			$model = new ContentModel();
            $id = $data['id'];
            unset($data['id']);
            if ($id <= 0) {
                $data['time'] = time();
                if ($model->insert($data)) {
                    return $this->jsonSuccess('添加成功');
                } else {
                    return $this->jsonError('添加失败');
                }
            } else {
                if ($model->update($data, ['id'=>$id]) !== false) {
                    return $this->jsonSuccess('修改成功');
                } else {
                    return $this->jsonError('修改失败');
                }
            }
		
		}

        $type = input('get.type', 0, 'intval');
        $id = input('id', 0, 'intval');
        $data = [];
        if ($id > 0) {
            $model = new ContentModel();
            $data = $model->where('id', $id)->find();
            $type = $data['type'];
            ($data['type'] == 4 || $data['type'] == 5) && $data['content'] = unserialize($data['content']);
        }
        $this->assign ('data', $data );
        !in_array($type, [1,2,3,4,5]) && $this->error('参数错误！');

        $classModel = new ContentclassModel();
        $classList = $classModel->order('sort desc,id desc')->select() ;
        $this->assign('classList', $classList);
        $this->assign('type', $type);
        return $this->fetch() ;
	}

    /**
     * 排序
     * */
    public function saveSort()
    {
        !isAjaxPost() && die;
        $model = new ContentModel();
        $sort = input('post.');
        foreach ($sort as $k => $v) {
            $model->where('id', $k)->update(['sort' => $v]);
        }
        return $this->jsonSuccess('修改成功');
    }
	
	/**
	 * 状态
	 */
	public function state()
	{
        !isAjaxPost() && die;
        $data = input('post.');
        $model = new ContentModel();
        if ($model->update(['state'=>$data['state']], ['id'=>$data['id']]) !== false) {
            return $this->jsonSuccess('修改成功');
        } else {
            return $this->jsonError('修改失败');
        }
	}
	
	/**
	 * 删除
	 */
	public function del()
	{
        !isAjaxPost() && die;
        $id = input('post.id');

        $model = new ContentModel();
        if ($id > 0 && $model->where('id', $id)->delete() !== false) {
            return $this->jsonSuccess('删除成功');
        } else {
            return $this->jsonError('删除失败');
        }
	}
	
}
